const db = wx.cloud.database()
const _ = db.command
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		team_Id:"",
		users:[],
		agree:"已同意",
		wait:"待同意",
		statusList:[]
	},
	change(e){
		console.log(e)
		let index = e.currentTarget.dataset.index
		if(this.data.statusList[index]){
			return
		}
		db.collection("team").doc(this.data.team_id).update({
			data:{
				teamPeople:_.push([this.data.users[index]._openid])
			}
		}).then(res => {
			let statusList = "statusList[" + index + "]"
			this.setData({
				[statusList]: 1
			})
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let teamId = options.teamid
		db.collection("team").where({teamId:teamId}).get().then(res=>{
			console.log(res)
			let applyPeople = res.data[0].applyPeople
			let teamPeople = res.data[0].teamPeople
			db.collection("users").where({
				_openid:_.in(applyPeople)
			}).get().then(eres => {
				console.log(eres)
				//把是否同意了的申请人员状态存入statusList
				let statusList = []
				for (let i = 0; i < eres.data.length; i++) {
					if (teamPeople.includes(eres.data[i]._openid)){
						statusList.push(1)
						continue
					}
					statusList.push(0)
				}
				this.setData({
					users: eres.data,
					statusList: statusList,
					team_id:res.data[0]._id
				})
		})
		})
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})